﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web.UI.WebControls.WebParts;
using IBS.Webparts.PostIt.Code.Interfaces;
using IBS.Webparts.PostIt.Code.Models;
using IBS.Webparts.PostIt.Code.Presenters;

namespace IBS.Webparts.PostIt.PostItWebpart
{
    [ToolboxItemAttribute(false)]
    public partial class PostItWebpart : WebPart, IPostItWebpart
    {
        // The following properties are configurable properties of the Webpart.  A custom editor is responsible
        // for displaying the UI for editing these fields.  Therefore, they are not marked as WebBrowsable and
        // automatically rendered using default Webpart property controls.


        /// <summary>
        /// The number of seconds to pause before rotating to the next set of slides.
        /// This property is a configurable property on the WebPart.
        /// </summary>
        [WebDisplayName("Pause Time"),
        WebDescription("The number of seconds to pause before rotating to the next set of slides."),
        Personalizable(PersonalizationScope.Shared)]
        public int PauseTime { get; set; }


        /// <summary>
        /// Whether to include a reference to the jQuery library in the Webpart.
        /// This property is a configurable property on the WebPart.
        /// </summary>
        [WebDisplayName("How to reference jQuery library."),
        WebDescription("The jQuery JavaScript library is required for this Webpart.  If a reference to jQuery exists in the Master Page, select MasterPage.  If no reference to jQuery exists in the Master Page, select Webpart."),
        Personalizable(PersonalizationScope.Shared)]
        public bool IncludejQueryReference { get; set; }


        /// <summary>
        /// Path to the jQuery library.
        /// This property is a configurable property on the WebPart.
        /// </summary>
        [WebDisplayName("Path to the jQuery library."),
        WebDescription("The path to the jQuery library."),
        Personalizable(PersonalizationScope.Shared)]
        public string jQueryPath { get; set; }


        /// <summary>
        /// Whether to include a reference to the bxSlider library in the Webpart.
        /// This property is a configurable property on the WebPart.
        /// </summary>
        [WebDisplayName("How to refrecne bxSlider library."),
        WebDescription("The bxSlider JavaScript library is required for this Webpart.  If a reference to jQuery exists in the Master Page, select MasterPage.  If no reference to jQuery exists in the Master Page, select Webpart."),
        Personalizable(PersonalizationScope.Shared)]
        public bool IncludebxSliderReference { get; set; }


        /// <summary>
        /// Path to the bxSlider library.
        /// This property is a configurable property on the WebPart.
        /// </summary>
        [WebDisplayName("Path to the bxSlider library."),
        WebDescription("The path to the bxSlider library."),
        Personalizable(PersonalizationScope.Shared)]
        public string bxSliderPath { get; set; }


        /// <summary>
        /// Path to the Css file for the webpart.
        /// This property is a configurable property on the WebPart.
        /// </summary>
        [WebDisplayName("Path to the CSS file for the webpart."),
        WebDescription("The path to the CSS file for the webpart."),
        Personalizable(PersonalizationScope.Shared)]
        public string CssPath { get; set; }
        

        /// <summary>
        /// Stores the Name of the List to get data to be displayed on this Webpart.
        /// This property is a configurable property on the WebPart.
        /// </summary>
        [Personalizable(PersonalizationScope.Shared)]
        public string ListName { get; set; }


        /// <summary>
        /// Stores the Guid of the List to get data to be displayed on this Webpart.
        /// This property is a configurable property on the WebPart.
        /// </summary>
        [Personalizable(PersonalizationScope.Shared)]
        public Guid ListGuid { get; set; }


        /// <summary>
        /// The name of the field in the list to use as the Title in the Webpart.
        /// This property is a configurable property on the WebPart.
        /// </summary>
        [Personalizable(PersonalizationScope.Shared)]
        public string TitleField { get; set; }


        /// <summary>
        /// The name of the field in the list to use as the StartDate in the Webpart.
        /// This property is a configurable property on the WebPart.
        /// </summary>
        [Personalizable(PersonalizationScope.Shared)]
        public string StartDateField { get; set; }


        /// <summary>
        /// The name of the field in the list to use as the EndDate in the Webpart.
        /// This property is a configurable property on the WebPart.
        /// </summary>
        [Personalizable(PersonalizationScope.Shared)]
        public string EndDateField { get; set; }


        /// <summary>
        /// Reference to the Presenter object
        /// </summary>
        private PostItWebpartPresenter _presenter;


        /// <summary>
        /// List of Post It Notes
        /// </summary>
        public List<PostItItem> PostItItems
        {
            get
            {
                if (postItRepeater == null)
                    return null;

                return (List<PostItItem>) postItRepeater.DataSource;
            }
            set
            {
                postItRepeater.DataSource = value;
                postItRepeater.DataBind();
            }
        }
        

        /// <summary>
        /// Initialize the PostIt Webpart control
        /// </summary>
        /// <param name="e"></param>
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            InitializeControl();
            _presenter = new PostItWebpartPresenter(this);
            _presenter.Initialize();
        }


        /// <summary>
        /// Loads the Control with the default values
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            BindAssets();
        }


        /// <summary>
        /// Bind assets
        /// </summary>
        private void BindAssets()
        {
            var sb = new StringBuilder();

            if (IncludejQueryReference)
                sb.AppendLine(BindScript(jQueryPath));

            if (IncludebxSliderReference)
                sb.AppendLine(BindScript(bxSliderPath));

            BindStyle(CssPath);

            ltBaseScripts.Text = sb.ToString();
        }


        /// <summary>
        /// Creates the markup to include a JavaScript reference
        /// </summary>
        /// <param name="scriptFileUrl">Name of the JavaScript file</param>
        /// <returns></returns>
        string BindScript(string scriptFileUrl)
        {
            return string.Format(@"<script type=""text/javascript"" src=""{0}""></script>", scriptFileUrl);
        }


        /// <summary>
        /// Creates the markup to include a CSS Style reference
        /// </summary>
        /// <param name="cssFileUrl">Name of the CSS file</param>
        /// <returns></returns>
        void BindStyle(string cssFileUrl)
        {
            CssRegistration1.Name = cssFileUrl;
        }


        /// <summary>
        /// Create the editor part
        /// </summary>
        /// <returns></returns>
        public override EditorPartCollection CreateEditorParts()
        {
            var editors = new List<EditorPart>
            {
                new PostItPropertiesEditorPart(ID, _presenter)
            };

            return new EditorPartCollection(editors);
        }


        /// <summary>
        /// Mark the Webpart as need changes to be saved.
        /// </summary>
        public void SaveChanges()
        {
            SetPersonalizationDirty();
        }
    }
}
